﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI.Report
{
    public partial class KFBusinessReport : BaseForm
    {
        public KFBusinessReport()
        {
            InitializeComponent();
        }

        private void KFBusinessReport_Load(object sender, EventArgs e)
        {
            this.winGridView1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView1.AppendedMenu = this.contextMenuStrip1;

            this.winGridView2.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView2.AppendedMenu = this.contextMenuStrip2;

            this.dtStart.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            this.dtEnd.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

            this.dtMonthStart.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            this.dtMonthEnd.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

        }

        private void BindDayTradeData()
        {
            #region 添加别名解析
            this.winGridView1.AddColumnAlias("BillNumber", "结账单号");
            this.winGridView1.AddColumnAlias("RoomNo", "房间号");
            this.winGridView1.AddColumnAlias("CustomerName", "宾客姓名");
            this.winGridView1.AddColumnAlias("Deposit", "押金");
            this.winGridView1.AddColumnAlias("Consumption", "消费金额");
            this.winGridView1.AddColumnAlias("OfferMoney", "优惠金额");
            this.winGridView1.AddColumnAlias("PayMoney", "实收金额");
            this.winGridView1.AddColumnAlias("KeepChange", "找零");
            this.winGridView1.AddColumnAlias("Creator", "收款人");
            this.winGridView1.AddColumnAlias("PayType", "收款方式");
            this.winGridView1.AddColumnAlias("Cash", "现金");
            this.winGridView1.AddColumnAlias("Card", "储值卡");
            this.winGridView1.AddColumnAlias("DaijinTicket", "代金劵");
            this.winGridView1.AddColumnAlias("NationalCredit", "国内信用卡");
            this.winGridView1.AddColumnAlias("InterCredit", "国际信用卡");
            this.winGridView1.AddColumnAlias("Invoice", "是否开发票");
            this.winGridView1.AddColumnAlias("CreateDate", "收款时间");
            this.winGridView1.AddColumnAlias("InHotelDate", "进店时间");
            this.winGridView1.AddColumnAlias("CheckStatus", "结账状态");
            this.winGridView1.AddColumnAlias("CreditCard", "信用卡号");
            this.winGridView1.AddColumnAlias("Note", "结账备注");
            #endregion

            #region 条件检索
            string status = "";
            if (this.radDayCheck.Checked)
            {
                status = CheckStatus.已结账.ToString();
            }
            else if (this.radDayFree.Checked)
            {
                status = CheckStatus.免费.ToString();
            }
            else if (this.radDayHold.Checked)
            {
                status = CheckStatus.签单.ToString();
            }

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("CheckStatus", status, SqlOperator.Like)
            .AddCondition("CreateDate", this.dtStart.Value.ToString(), SqlOperator.MoreThanOrEqual)
            .AddCondition("CreateDate", this.dtEnd.Value.ToString(), SqlOperator.LessThanOrEqual);
            string filter = condition.BuildConditionSql(DatabaseType.SqlServer);
            #endregion

            DataTable dt = BLLFactory<RoomBill>.Instance.GetCheckOutReport(filter);
            #region 增加汇总信息
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr["BillNumber"] = string.Format("账单数:{0}", dt.Rows.Count);
                decimal consumption = 0M;
                decimal offerMoney = 0M;
                decimal PayMoney = 0M;
                decimal KeepChange = 0M;
                decimal cash = 0M;
                decimal card = 0M;
                decimal daijin = 0M;
                decimal nationalCredit = 0M;
                decimal interCredit = 0M;
                foreach (DataRow row in dt.Rows)
                {
                    consumption += Convert.ToDecimal(row["Consumption"]);
                    offerMoney += Convert.ToDecimal(row["OfferMoney"]);
                    PayMoney += Convert.ToDecimal(row["PayMoney"]);
                    KeepChange += Convert.ToDecimal(row["KeepChange"]);
                    cash += Convert.ToDecimal(row["Cash"]);
                    card += Convert.ToDecimal(row["Card"]);
                    daijin += Convert.ToDecimal(row["DaijinTicket"]);
                    nationalCredit += Convert.ToDecimal(row["NationalCredit"]);
                    interCredit += Convert.ToDecimal(row["InterCredit"]);
                }
                dr["Consumption"] = consumption;
                dr["OfferMoney"] = offerMoney;
                dr["PayMoney"] = PayMoney;
                dr["KeepChange"] = KeepChange;
                dr["Cash"] = cash;
                dr["Card"] = card;
                dr["DaijinTicket"] = daijin;
                dr["NationalCredit"] = nationalCredit;
                dr["InterCredit"] = interCredit;

                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dr);
            } 
            #endregion
            this.winGridView1.DataSource = dt.DefaultView;
            this.winGridView1.PrintTitle = Portal.gc.gAppUnit + " -- " + "客房日营业额查询报表";
        }
        private void BindMonthTradeData()
        {
            #region 添加别名解析
            this.winGridView2.AddColumnAlias("CreateDate2", "结算日期");
            this.winGridView2.AddColumnAlias("Consumption", "消费金额");
            this.winGridView2.AddColumnAlias("OfferMoney", "优惠金额");
            this.winGridView2.AddColumnAlias("PayMoney", "实收金额");
            this.winGridView2.AddColumnAlias("KeepChange", "找零");
            this.winGridView2.AddColumnAlias("Cash", "现金");
            this.winGridView2.AddColumnAlias("Card", "储值卡");
            this.winGridView2.AddColumnAlias("DaijinTicket", "代金劵");
            this.winGridView2.AddColumnAlias("NationalCredit", "国内信用卡");
            this.winGridView2.AddColumnAlias("InterCredit", "国际信用卡");
            #endregion

            #region 条件检索
            string status = "";
            if (this.radMonthCheck.Checked)
            {
                status = CheckStatus.已结账.ToString();
            }
            else if (this.radMonthFree.Checked)
            {
                status = CheckStatus.免费.ToString();
            }
            else if (this.radMonthHold.Checked)
            {
                status = CheckStatus.签单.ToString();
            }

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("CheckStatus", status, SqlOperator.Like)
            .AddCondition("CreateDate", this.dtMonthStart.Value.ToString(), SqlOperator.MoreThanOrEqual)
            .AddCondition("CreateDate", this.dtMonthEnd.Value.ToString(), SqlOperator.LessThanOrEqual);
            string filter = condition.BuildConditionSql(DatabaseType.SqlServer);
            #endregion

            DataTable dt = BLLFactory<RoomBill>.Instance.GetMonthTradeReport(filter);
            #region 增加汇总信息
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr["CreateDate2"] = string.Format("统计天数:{0}", dt.Rows.Count);
                decimal consumption = 0M;
                decimal offerMoney = 0M;
                decimal PayMoney = 0M;
                decimal KeepChange = 0M;
                decimal cash = 0M;
                decimal card = 0M;
                decimal daijin = 0M;
                decimal nationalCredit = 0M;
                decimal interCredit = 0M;
                foreach (DataRow row in dt.Rows)
                {
                    consumption += Convert.ToDecimal(row["Consumption"]);
                    offerMoney += Convert.ToDecimal(row["OfferMoney"]);
                    PayMoney += Convert.ToDecimal(row["PayMoney"]);
                    KeepChange += Convert.ToDecimal(row["KeepChange"]);
                    cash += Convert.ToDecimal(row["Cash"]);
                    card += Convert.ToDecimal(row["Card"]);
                    daijin += Convert.ToDecimal(row["DaijinTicket"]);
                    nationalCredit += Convert.ToDecimal(row["NationalCredit"]);
                    interCredit += Convert.ToDecimal(row["InterCredit"]);
                }
                dr["Consumption"] = consumption;
                dr["OfferMoney"] = offerMoney;
                dr["PayMoney"] = PayMoney;
                dr["KeepChange"] = KeepChange;
                dr["Cash"] = cash;
                dr["Card"] = card;
                dr["DaijinTicket"] = daijin;
                dr["NationalCredit"] = nationalCredit;
                dr["InterCredit"] = interCredit;

                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dr);
            } 
            #endregion
            this.winGridView2.DataSource = dt.DefaultView;
            this.winGridView2.PrintTitle = Portal.gc.gAppUnit + " -- " + "客房月营业额查询报表";
        }

        private void btnDaySearch_Click(object sender, EventArgs e)
        {
            BindDayTradeData();
        }

        private void btnMonthSearch_Click(object sender, EventArgs e)
        {
            BindMonthTradeData();
        }

        private void radDay_CheckedChanged(object sender, EventArgs e)
        {
            BindDayTradeData();
        }
        private void radMonth_CheckedChanged(object sender, EventArgs e)
        {
            BindMonthTradeData();
        }
    }
}
